package com.example.projecth.water2;

import java.util.LinkedList;

import com.example.projecth.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class WaterCheck extends Activity {
	private final int ACTIVITY_RESULT_OK = 1;
	private final int WRITE_ACTIVITY_REQUEST = 1;
	private Button start,restart;
	private ListView memoView;

	private ArrayAdapter<Memo> adapter;
	private LinkedList<Memo> memoList = new LinkedList<Memo>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.w_water_check_view);
		restart = (Button) findViewById(R.id.restart);

		start = (Button) findViewById(R.id.start);
		memoView = (ListView) findViewById(R.id.memolist);
		adapter = new ArrayAdapter<Memo>(this,
				android.R.layout.simple_list_item_1, memoList);
		memoView.setAdapter(adapter);

		restart.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (memoList != null) {
				memoList.removeAll(memoList);
				adapter.notifyDataSetChanged();
				}
			}
		});
		start.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(WaterCheck.this, WaterRegister.class);
				startActivityForResult(intent, WRITE_ACTIVITY_REQUEST);

			}
		});
		

		return;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == WRITE_ACTIVITY_REQUEST) {
			if (resultCode == ACTIVITY_RESULT_OK) {
				Memo memo = (Memo) data.getSerializableExtra("newmemo");
				memoList.add(memo);
				adapter.notifyDataSetChanged();
			}
		}
	}

}
